<?php
declare (strict_types=1);

namespace app\api\version\v1;

use app\common\controller\Api;
use app\common\lib\Poster;
use app\common\validate\UserUpdateAuthValidate;
use think\facade\Db;
use think\facade\Env;
use think\PHPQRImgCode;
use think\response\Json;

/**
 * | Notes：会员中心
 * +----------------------------------------------------------------------
 * | PHP Version 7.2+
 * +----------------------------------------------------------------------
 * | Copyright (c) 2011-2020 https://www.xxq.com.cn, All rights reserved.
 * +----------------------------------------------------------------------
 * | Author: 和好 <389123492@qq.com>
 * +----------------------------------------------------------------------
 * | Date: 2020/11/3 18:05
 * +----------------------------------------------------------------------
 */
class Member extends Api
{
    use Poster;


    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
    }

    /**
     * @OA\Post(path="/v1/user",
     *   tags={"用户管理"},
     *   summary="用户中心",
     *   @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string", default="123456")),
     *     @OA\RequestBody(
     *     @OA\MediaType(
     *       mediaType="multipart/form-data"
     *       )
     *     ),
     *   @OA\Response(
     *      response=200,
     *      description="The Member",
     *      @OA\JsonContent(type="object",
     *          @OA\Property(property="msg", type="string"),
     *          @OA\Property(property="code", type="integer", default="200"),
     *          @OA\Property(property="data", type="object",
     *               @OA\Property(property="认证步骤 0未完成任何认证 1实名认证 2支付授权 3地址认证", type="integer", description="1"),
     *          ),
     *      ),
     *   ),
     * )
     */
    public function index(): Json
    {
        $member = Db::name('member')
            ->alias('member')
            ->field('member.*,member_extend.introduce,member_extend.real_name_status')
            ->leftJoin('think_member_extend member_extend', 'member_extend.member_id=member.id')
            ->where('member.id', $this->uuid)
            ->find();
        // 绑定地址
        $data = [
            'member' => [
                'mobile'           => $member['mobile'],
                'nickname'         => $member['nickname'],
                'number'           => strtoupper($member['code']),
                'code'             => $member['code'],
                'account_address'  => $member['account_address'],
                'introduce'        => $member['introduce'],
                "portrait"         => $member['portrait'],                      //头像
                'money'            => $member['money'],
                'invite_count'     => $member['invite_count'],                  //直推人数
                'team_count'       => $member['team_count'],                    //团队人数
                'integral'         => $member['integral'],                      //剩余积分数量
                'create_time'      => $member['create_time'],                   //注册时间
                'status'           => $member['status'],                        //用户状态
                'cast_role'        => $member['cast_role'],
                'real_name_status' => $member['real_name_status'],              //用户状态

            ],
        ];
        return self::jsonSuccessData("获取成功", $data);
    }

    /**
     * @OA\Post(path="/v1/setMemberInfo",
     *   tags={"用户管理"},
     *   summary="更新用户信息",
     *   @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string", default="123456")),
     *     @OA\RequestBody(
     *     @OA\MediaType(
     *       mediaType="multipart/form-data",
     *         @OA\Schema(
     *           @OA\Property(description="用户头像", property="portrait", type="string", default=""),
     *           @OA\Property(description="昵称", property="nickname", type="string", default=""),
     *           required={"portrait","nickname"})
     *       )
     *     ),
     *   @OA\Response(
     *      response=200,
     *      description="The Member",
     *      @OA\JsonContent(type="object",
     *          @OA\Property(property="msg", type="string"),
     *          @OA\Property(property="code", type="integer", default="200"),
     *          @OA\Property(property="data", type="object",
     *          ),
     *      ),
     *   ),
     * )
     */
    public function setMemberInfo(): Json
    {
        $member_option = [
            'portrait'    => $this->post['portrait'],
            'nickname'    => $this->post['nickname'],
            'update_time' => date("Y-m-d H:i:s"),
        ];
        validate(UserUpdateAuthValidate::class)
            ->check($member_option);

        $result = Db::name('member')
            ->where('id', $this->uuid)
            ->update($member_option);

        return self::jsonSyncSuccessData("修改成功");
    }

    /**
     * @OA\Post(path="/v1/inviteFriends",
     *   tags={"用户管理"},
     *   summary="分享好友",
     *   @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string", default="123456")),
     *     @OA\RequestBody(
     *     @OA\MediaType(
     *       mediaType="multipart/form-data",
     *       )
     *     ),
     *   @OA\Response(
     *      response=200,
     *      description="The Member",
     *      @OA\JsonContent(type="object",
     *          @OA\Property(property="msg", type="string"),
     *          @OA\Property(property="code", type="integer", default="200"),
     *          @OA\Property(property="data", type="object",
     *          ),
     *      ),
     *   ),
     * )
     */
    public function inviteFriends(): Json
    {
        ini_set('gd.jpeg_ignore_warning', '1');


        // 获取用户ID
        $uuid = $this->uuid;
        // 创建二维码类
        $qrcode_obj = new PHPQRImgCode();
        // 生成二维码图片路径及名称
        $qr_code_path = 'uploads/poster/' . $uuid . '/invite_qrcode.jpg';

        self::mkdirs('uploads/poster/' . $uuid);

        // 配置信息
        $config = [
            'ecc'                => 'Q',                        // L-smallest, M, Q, H-best
            'size'               => 12,                         // 1-50
            'dest_file'          => $qr_code_path,
            'quality'            => 90,
            'logo'               => '',
            'logo_size'          => 80,
            'logo_outline_size'  => 15,
            'logo_outline_color' => '#FFFFFF',
            'logo_radius'        => 20,
            'logo_opacity'       => 100
        ];

        // 设定配置
        $qrcode_obj->set_config($config);

        // 小程序二维码地址
        $short_url = Env::get('app.www_project_url') . 'reg/' . self::$user_cache['code'];

        // 创建二维码
        $qrcode_obj->generate($short_url);

        return self::jsonSuccessData('获取成功', [
            'poster_path' => Env::get('app.www_project_url') . $qr_code_path,
            'code'        => self::$user_cache['code'],
            'poster_url'  => $short_url,
        ]);
    }

}